package com.samsungmobile.common.util;

import java.io.*;
import com.samsungmobile.common.Logger;

public class EasyFile
{

    private EasyFile()
    {
    }

    public static String getFileSizeString(int i)
    {
        String as[] = {
            " B", " KB", " MB", " GB"
        };
        boolean flag = false;
        String s = "";
        String s1 = "";
        if(i < 0)
            return "";
        if(i < 1000)
        {
            int j = i;
            if(j <= 0)
                s = "1 B";
            else
                s = j + " B";
        } else
        if(i < 0xf4240)
        {
            int k = i / 1000;
            if(k <= 0)
                s = "1 KB";
            else
                s = k + " KB";
        } else
        if(i < 0x3b9aca00)
        {
            int l = i / 0xf4240;
            if(l <= 0)
                s = "1 MB";
            else
                s = l + " MB";
        } else
        {
            int i1 = (int)((long)i / 0x3b9aca00L);
            if(i1 <= 0)
                s = "1 GB";
            else
                s = i1 + " GB";
        }
        return s;
    }

    public static String getFileEXT(String s)
    {
        String s1 = "";
        s1 = getFileName(s);
        int i = s1.indexOf(".");
        if(i > 0)
            s1 = s1.substring(i + 1);
        else
            s1 = "";
        return s1;
    }

    public static String getFileName(String s)
    {
        String s1 = s;
        int i = s.lastIndexOf("\\");
        if(i > 0)
        {
            s1 = s.substring(i + 1);
        } else
        {
            int j = s.lastIndexOf("/");
            if(j > 0)
                s1 = s.substring(j + 1);
        }
        return s1;
    }

    public static String getFileNameOnly(String s)
    {
        String s1 = "";
        s1 = getFileName(s);
        int i = s1.indexOf(".");
        if(i > 0)
            s1 = s1.substring(0, i);
        return s1;
    }

    public static boolean makeFolder(String s)
    {
        File file = new File(s);
        return file.mkdirs();
    }

    public static void deleteFolder(String s, boolean flag)
        throws FileNotFoundException, IOException
    {
        File file = new File(s);
        deleteFolder(file, flag);
        if(flag)
            file.delete();
    }

    private static void deleteFolder(File file, boolean flag)
        throws FileNotFoundException, IOException
    {
        try
        {
            File afile[] = file.listFiles();
            int i = afile.length;
            for(int j = 0; j < i; j++)
                if(afile[j].isDirectory())
                {
                    if(flag)
                    {
                        deleteFolder(afile[j], flag);
                        afile[j].delete();
                    }
                } else
                {
                    afile[j].delete();
                }

        }
        catch(NullPointerException nullpointerexception)
        {
            throw new FileNotFoundException();
        }
    }

    public static boolean deleteFile(String s)
    {
        boolean flag = false;
        try
        {
            File file = new File(s);
            flag = file.delete();
        }
        catch(NullPointerException nullpointerexception)
        {
        	Logger.printError(nullpointerexception);
        }
        return flag;
    }

    public static void writeFile(String s, String s1)
        throws FileNotFoundException, IOException
    {
        writeFile(s, s1, false);
    }

    public static void writeFile(String s, String s1, boolean flag)
        throws FileNotFoundException, IOException
    {
        Object obj = null;
        Object obj1 = null;
        try
        {
            FileOutputStream fileoutputstream = new FileOutputStream(s, flag);
            PrintStream printstream = new PrintStream(fileoutputstream);
            printstream.println(s1);
            printstream.close();
            printstream = null;
        }
        catch(FileNotFoundException filenotfoundexception)
        {
        	Logger.printError(filenotfoundexception);
            throw filenotfoundexception;
        }
        catch(IOException ioexception)
        {
            Logger.printError(ioexception);
            throw ioexception;
        }
    }

    public static String readFile(String s)
        throws FileNotFoundException, IOException
    {
        String s1 = null;
        Object obj = null;
        Object obj1 = null;
        try
        {
            s1 = "";
            FileReader filereader = new FileReader(s);
            BufferedReader bufferedreader = new BufferedReader(filereader);
            for(String s2 = ""; (s2 = bufferedreader.readLine()) != null;)
                s1 = s1 + s2 + "\r\n";

            filereader.close();
            filereader = null;
            bufferedreader.close();
            bufferedreader = null;
        }
        catch(FileNotFoundException filenotfoundexception)
        {
        	Logger.printError(filenotfoundexception);
            throw filenotfoundexception;
        }
        catch(IOException ioexception)
        {
        	Logger.printError(ioexception);
            throw ioexception;
        }
        return s1;
    }

    public static String readFile(String s, String chaSet)
    throws FileNotFoundException, IOException
	{
	    String s1 = null;
	    Object obj = null;
	    Object obj1 = null;
	    try
	    {
	        s1 = "";
	        FileReader filereader = new FileReader(s);
	        BufferedReader bufferedreader = new BufferedReader(filereader);
	        
	        for(String s2 = ""; (s2 = bufferedreader.readLine()) != null;)
	        	s1 = s1 + new String(s2.getBytes(),chaSet) + "\r\n";
	
	        filereader.close();
	        filereader = null;
	        bufferedreader.close();
	        bufferedreader = null;
	    }
	    catch(FileNotFoundException filenotfoundexception)
	    {
	    	Logger.printError(filenotfoundexception);
	        throw filenotfoundexception;
	    }
	    catch(IOException ioexception)
	    {
	    	Logger.printError(ioexception);
	        throw ioexception;
	    }
	    return s1;
	}
    
    public static void createFileAndWrite(String s, String s1)
    {
        try
        {
            BufferedWriter bufferedwriter = new BufferedWriter(new FileWriter(s));
            bufferedwriter.write(s1);
            bufferedwriter.close();
        }
        catch(IOException ioexception)
        {
            System.out.println("  in icg.io.EasyFile.createFileAndWrite() : " + ioexception.toString());
        }
    }

    public static void copyFile(File file, File file1)
        throws FileNotFoundException, IOException
    {
        FileInputStream fileinputstream = null;
        FileOutputStream fileoutputstream = null;
        byte abyte0[] = new byte[1024];
        boolean flag = false;
        try
        {
            fileinputstream = new FileInputStream(file);
            if(file1.isDirectory())
            {
                fileoutputstream = new FileOutputStream(new File(file1, file.getName()));
            } else
            {
                File file2 = file1.getParentFile();
                if(!file2.exists())
                    file2.mkdirs();
                fileoutputstream = new FileOutputStream(file1);
            }
            int i;
            while((i = fileinputstream.read(abyte0)) != -1) 
                fileoutputstream.write(abyte0, 0, i);
        }
        finally
        {
            if(fileinputstream != null)
                try
                {
                    fileinputstream.close();
                }
                catch(Exception exception1) { }
            if(fileoutputstream != null)
                try
                {
                    fileoutputstream.close();
                }
                catch(Exception exception2) { }
        }
    }

    public static void copyFile(String s, String s1)
        throws FileNotFoundException, IOException
    {
        copyFile(new File(s), new File(s1));
    }

    public static boolean move(File file, File file1)
    {
        boolean flag = true;
        try
        {
            copyFile(file, file1);
        }
        catch(Exception exception)
        {
            flag = false;
        }
        file.delete();
        return flag;
    }

    public static String getExtFromFileName(String s)
    {
        return s.substring(s.lastIndexOf(".") + 1);
    }
}
